package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    @PostMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R addSetmeal(@RequestBody SetmealDto setmealDto) {

        setmealService.addWithDish(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R getPage(Integer page, Integer pageSize, String name) {

        IPage<Setmeal> pageinfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        setmealService.page(pageinfo, qw);

        List<Setmeal> records = pageinfo.getRecords();
        long total = pageinfo.getTotal();

        List<SetmealDto> setmeals = new ArrayList<>();
        for (Setmeal setmeal : records) {
            SetmealDto setmealDto = new SetmealDto();
            //根据categoryId查询categoryName
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            //根据setmeal_id查询setmealDishes
            /*LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> setmealDishes = setmealDishService.list(lqw);*/
            //为setmealDto设置参数
            BeanUtils.copyProperties(setmeal, setmealDto);
            setmealDto.setCategoryName(categoryName);
            //setmealDto.setSetmealDishes(setmealDishes);

            setmeals.add(setmealDto);
        }
        Map<Object, Object> data = new HashMap<>();
        data.put("records", setmeals);
        data.put("total", total);
        return R.success(data);
    }

    @DeleteMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R deleteSetmeal(@RequestParam List<Long> ids) {

        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        qw.eq(Setmeal::getStatus,1);
        int count = setmealService.count(qw);

        if(count != 0 ){
            return R.error("套餐售卖中,不可删除");
        }

        //删除套餐内的dish
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lqw);

        //删除套餐
        setmealService.removeByIds(ids);

        return R.success("套餐删除成功");
    }

    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmeal",allEntries = true)
    public R updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        LambdaUpdateWrapper<Setmeal> luw = new LambdaUpdateWrapper<>();
        luw.in(Setmeal::getId,ids);
        luw.set(Setmeal::getStatus,status);
        setmealService.update(luw);

        return R.success("操作成功");
    }

    //回显数据
    @GetMapping("/{sid}")
    public R getSetmeal(@PathVariable String sid){
        Long id = Long.parseLong(sid);
        Setmeal setmeal = setmealService.getById(id);

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(lqw);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);

        return R.success(setmealDto);
    }

    @PutMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R updateSetmeal(@RequestBody SetmealDto setmealDto){
        //更新基本数据
        setmealService.updateById(setmealDto);

        //删除旧的settmealDishes数据
        LambdaUpdateWrapper<SetmealDish> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lqw);

        //添加新的settmealDishes数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);



        return R.success(null);
    }

    @GetMapping("/list")
    @Cacheable(value = "setmeal",key = "#categoryId+':'+ #status")
    public R getByCategoryId(@RequestParam Long categoryId,Integer status){
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,categoryId);
        lqw.eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(lqw);
        if(list != null && list.size() >0){
            return R.success(list);
        }
        return R.error("该分类下无套餐");
    }

    @GetMapping("/dish/{id}")
    public R getDish(@PathVariable Long id){

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(lqw);

        ArrayList<SetmealDishDto> dishes = new ArrayList<>();
        for (SetmealDish setmealDish : list) {
            SetmealDishDto setmealDishDto = new SetmealDishDto();
            BeanUtils.copyProperties(setmealDish,setmealDishDto);

            Dish dish = dishService.getById(setmealDish.getDishId());
            String image = dish.getImage();
            setmealDishDto.setImage(image);

            dishes.add(setmealDishDto);
        }
        return R.success(dishes);
    }
}

